package com.zhitan.ems.app.api.patrol;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhitan.ems.app.patrol.entity.PatrolEntity;
import com.zhitan.ems.app.patrol.entity.PatrolPointEntity;
import com.zhitan.ems.app.patrol.entity.PatrolTrackEntity;
import com.zhitan.ems.app.patrol.service.PatrolPointService;
import com.zhitan.ems.app.patrol.service.PatrolService;
import com.zhitan.ems.app.patrol.service.PatrolTrackService;
import com.zhitan.ems.app.patrol.vo.PatrolVO;
import com.zhitan.ems.app.patrol.wrapper.PatrolWrapper;
import com.zhitan.ems.common.core.domain.R;
import com.zhitan.ems.common.utils.StringUtils;
import com.zhitan.ems.framework.core.vo.VPage;
import com.zhitan.ems.framework.core.vo.VQueryKit;
import com.zhitan.ems.mini.api.ApiBaseAction;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
 * 巡检项目
 * @author zhitan
 * 
 * @date 2024-11-02 14:05:23
 */
@Api(tags = "App管理--巡检项目", value = "巡检项目")
@RestController
@RequestMapping("api/patrol/patrol")
public class ApiPatrolController extends ApiBaseAction{
    @Autowired
    private PatrolService patrolService;
    @Autowired
    private PatrolPointService patrolPointService;
    @Autowired
    private PatrolTrackService patrolTrackService;
    /**
     * 列表
     */
    @ApiOperation(value = "列表", response = VPage.class)
    @ApiImplicitParams({
		        @ApiImplicitParam(name = "name", value = "标题",  paramType = "query", required = false, dataType =  "String"),
		})
    @GetMapping("/browse")
    public R<VPage<PatrolVO>> browse(@RequestParam Map<String, Object> params){
        QueryWrapper<PatrolEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), PatrolEntity.class);
        queryWrapper.orderByDesc("id");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
        IPage<PatrolEntity> pages = patrolService.page(VQueryKit.getPage(params), queryWrapper);
        return R.data(PatrolWrapper.build().toVPageVO(pages));
    }
    /**
     * 信息
     */
    @ApiOperation(value = "信息", response = PatrolVO.class)
    @GetMapping("/detail/{id}")
    public R<PatrolVO> detail(@PathVariable("id") Long id){
		PatrolEntity patrol = patrolService.getById(id);
		PatrolVO patrolVO=PatrolWrapper.build().entityVO(patrol);
		QueryWrapper<PatrolTrackEntity> queryWrapper=new QueryWrapper<PatrolTrackEntity>();
		queryWrapper.eq("patrol_id", id);
        queryWrapper.orderByAsc("id");
        List<PatrolTrackEntity> patrolTrackList = patrolTrackService.list(queryWrapper);
        patrolVO.setPatrolTrackList(patrolTrackList);
	    return R.data(patrolVO);
    }
    @ApiOperation(value = "扫码节点", response = PatrolVO.class)
    @GetMapping("/scanPoint")
    public R<PatrolPointEntity> scanPoint(@RequestParam(value = "qrCode") String qrCode){
    	PatrolPointEntity patrolPoint = patrolPointService.queryByQrCode(qrCode);
    	return R.data(patrolPoint);
    }
    
}
